<?php
namespace app\common\model\orderreceipt;

use think\Model;
use think\facade\Session;
use app\common\model\user\User;

class Receipt extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'Receipt';
        protected $pk = 'ReceiptId';
        
        const STATEOK  = 0;
        const STATEINT = 1;
    public function goodInfo(){
        return $this->hasOne('app\common\model\orderdata\Goods','GoodId','GoodId');
    }
    /**
     * 获取危废订单
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getlist($data,$type='ware'){
            
        $ndata = Receipt::where('ReceiptListId',$data['ReceiptListId'])
                    ->order(['GoodId','GoodName'])
                    ->field('GoodName,GoodId,SUM(Weight) as Weight,ReceiptListId,Price,concat(GoodId,GoodName) as group_id_name')
                    ->group('group_id_name')
                    ->with('goodInfo')
                    ->select();
        $ndata->hidden(['ReceiptId','good_info.OperationLog','good_info.TotalWeight'])->toArray();
        return $ndata;
    }

    /**
     * 增加订单
     * @param  [type] $Receiptlistid [总订单id]
     * @param  [type] $goodlist   产品数组
     * @param  [type] $valuelist  重量数组
     * @return [type]             [description]
     */
    public static function screate($ReceiptListId,$goodlist,$valuelist,$pricelist,$goodnamelist,$orderlist){
        if(empty($ReceiptListId)){
           return serr('缺少订单id');
        }
        if(count($goodlist)<count($valuelist)){
           return serr('订单参数不匹配');
        }
        $cdata = [];
        $ndata['ReceiptListId'] = $ReceiptListId;
        foreach ($valuelist as $key => $value) {
            $ndata['GoodId'] = $goodlist[$key];
            $ndata['Weight'] = $valuelist[$key];
            $ndata['Price'] = $pricelist[$key];
            $ndata['GoodName'] = $goodnamelist[$key];
            $ndata['HawaId'] = $orderlist[$key];
            $cdata[] = $ndata;
        }
        $Receipt = new Receipt;
        $data = $Receipt->saveAll($cdata);
        return sok();
    }


    /**
     * 判断列表中的产品状态是否可操作
     * @param  [type] $list [id数组]
     * @return [type]       [返回错误代码]
     */
    public static function list_state($list){
        $data = Goods::where('State','<>',Goods::STATEOK)->where('GoodId','in',$list)->column('OrderNumber');
        if(!empty($data)){
            $str = implode("\n", $data);
            $str = "以下危废无法使用：\n" . $str;
            return serr($str);
        }else{
            return sok();
        }
    }
   

   
}
